/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Documents;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 *
 * @author Mukesh
 */
public class EditAdmissionDocument extends HttpServlet {

    HashMap htmlfields = new HashMap();
    HashMap fileNames = new HashMap();
    int i = 0;
    String tempFileName = "";

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        int admissionId = 0;
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if (isMultipart) {
            try {
                ServletFileUpload servlet = new ServletFileUpload();
                FileItemIterator fit = servlet.getItemIterator(request);
                while (fit.hasNext()) {
                    FileItemStream f = fit.next();
                    if (f.isFormField()) {
                        InputStream input = f.openStream();
                        byte b[] = new byte[input.available()];
                        input.read(b);
                        String value = new String(b);
                        htmlfields.put(f.getFieldName(), value);
                        tempFileName = f.getFieldName();
                    } else {
                        if (f.getName().isEmpty()) {
                            continue;
                        } else {
                            String fileName = (String) htmlfields.get(tempFileName);
                            String fullFileName = DocUpload.FileUpload.writeFile(f, fileName);
                            fileNames.put(tempFileName, fullFileName);
                        }
                    }
                }
            } catch (Exception e) {
                session.setAttribute("msg", "Try Again...! Not Done.(" + e + ")");
                session.setAttribute("status", "fail");
                response.sendRedirect("/RIT/Services/Admission/EditAdmission.jsp");
            }
        }
        try {
//-----------------------------------------------------------------
            Vector<Object> list = new Vector<Object>();

            if (htmlfields.get("admissionId").toString().length() != 0) {
                admissionId = Integer.parseInt(htmlfields.get("admissionId").toString());
            }
            String[] token = htmlfields.get("docid").toString().split("/");
            for (int k = 0; k < token.length; k++) {
                if (token[k].length() != 0) {
                    StudentDocumentBean sdb = new StudentDocumentBean();
                    if (htmlfields.get(token[k] + "-t") == null) {
                        continue;
                    }
                    sdb.setDocumentId(Integer.parseInt(token[k]));
                    sdb.setDocumentNo(htmlfields.get(token[k] + "-t").toString());
                    sdb.setAdmissionId(admissionId);
                    sdb.setEntryEmail(session.getAttribute("uid").toString());
                    if (fileNames.get(token[k] + "-t") != null) {
                        sdb.setDocumentName(fileNames.get(token[k] + "-t").toString());
                    }

                    list.add(sdb);
                }
            }
            int stuDoc = Documents.Query.addStuDocs(list);
            if (stuDoc != 0) {
                session.setAttribute("msg", "Documents Uploaded Successfully.");
                session.setAttribute("status", "success");
                response.sendRedirect("/RIT/Services/Admission/Admission.jsp?goto=6&admissionId=" + admissionId);
            } else {
                session.setAttribute("msg", "Check Documents & try again.");
                session.setAttribute("status", "success");
                response.sendRedirect("/RIT/Services/Admission/Admission.jsp?goto=5&admissionId=" + admissionId);
            }

        } catch (Exception e) {
            session.setAttribute("msg", "Check Documents & try again.(e)");
            session.setAttribute("status", "success");
            response.sendRedirect("/RIT/Services/Admission/Admission.jsp?goto=5&admissionId=" + admissionId);
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
